package com.snack.system.service.impl;

import com.snack.common.core.constant.UserConstants;
import com.snack.common.core.utils.StringUtils;
import com.snack.common.core.utils.dif.DifUtils;
import com.snack.common.core.utils.uuid.IdUtils;
import com.snack.system.domain.TsGoods;
import com.snack.system.domain.TsGoodsParamData;
import com.snack.system.mapper.TsGoodsMapper;
import com.snack.system.mapper.TsGoodsParamDataMapper;
import com.snack.system.service.ITsGoodsService;
import com.snack.system.service.ITsGoodsTypeService;
import com.snack.system.service.ITsResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 商品Service业务层处理
 * 
 * @author snack
 * @date 2022-09-18
 */
@Service
public class TsGoodsServiceImpl implements ITsGoodsService 
{
    @Autowired
    private TsGoodsMapper goodsMapper;
    @Autowired
    private TsGoodsParamDataMapper goodsParamDataMapper;

    @Autowired
    private ITsGoodsTypeService goodsTypeService;

    @Autowired
    private ITsResourceService resourceService;

    /**
     * 查询商品
     * 
     * @param goodsId 商品主键
     * @return 商品
     */
    @Override
    public TsGoods selectById(Long goodsId)
    {
        return goodsMapper.selectById(goodsId);
    }

    /**
     * 查询名称唯一
     * @param goods
     * @return
     */
    @Override
    public String checkNameUnique(TsGoods goods){
        Long goodsId = StringUtils.isNull(goods.getGoodsId()) ? -1L : goods.getGoodsId();
        TsGoods info = goodsMapper.checkNameUnique(goods.getGoodsName(),goods.getGoodsTypeId());
        if (StringUtils.isNotNull(info) && info.getGoodsId().longValue() != goodsId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 查询编码唯一
     * @param goods
     * @return
     */
    @Override
    public String checkCodeUnique(TsGoods goods){
        Long goodsId = StringUtils.isNull(goods.getGoodsId()) ? -1L : goods.getGoodsId();
        TsGoods info = goodsMapper.selectByCode(goods.getCode());
        if (StringUtils.isNotNull(info) && info.getGoodsId().longValue() != goodsId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 查询商品列表
     * 
     * @param goods 商品
     * @return 商品
     */
    @Override
    public List<TsGoods> selectList(TsGoods goods)
    {
        return goodsMapper.selectList(goods);
    }

    /**
     * 新增商品
     * 
     * @param goods 商品
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public int insert(TsGoods goods)
    {
        //设置pics
        if (StringUtils.isNotEmpty(goods.getResourceList())){
            String pics = IdUtils.fastSimpleUUID();
            goods.setPics(pics);
            goods.getResourceList().forEach((item)-> item.setPics(pics));
        }
        int rows = goodsMapper.insert(goods);

        //插入设备架构关联
        insertParamData(goods);
        // 新增资源
        resourceService.insertBatch(goods.getResourceList());
        return rows;
    }

    /**
     * 修改商品
     * 
     * @param goods 商品
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public int update(TsGoods goods)
    {
        //更改equipGoodsData
        updateParamData(goods);
        //添加/删除资源
        String pics = StringUtils.isNotEmpty(goods.getPics()) ? goods.getPics() : IdUtils.fastSimpleUUID();
        resourceService.updateBatch(goods.getResourceList(),pics);
        goods.setPics(pics);
        return goodsMapper.update(goods);
    }

    /**
     * 修改状态
     *
     * @param goods 商品
     * @return 结果
     */
    @Override
    public int updateStatus(TsGoods goods){
        return goodsMapper.update(goods);
    }

    /**
     * 批量删除商品
     * 
     * @param goodsIds 需要删除的商品主键
     * @return 结果
     */
    @Override
    public int deleteByIds(Long[] goodsIds)
    {
        return goodsMapper.deleteByIds(goodsIds);
    }


    private void insertParamData(TsGoods goods){
        if(StringUtils.isNotEmpty(goods.getGoodsParamDataList())){
            batchGoodsId(goods);
            goodsParamDataMapper.insertBatch(goods.getGoodsParamDataList());
        }
    }

    private void updateParamData(TsGoods goods){
        if(StringUtils.isNotEmpty(goods.getGoodsParamDataList())){
            batchGoodsId(goods);
            List<TsGoodsParamData> oldGoodsParamDataList = goodsParamDataMapper.selectListByGoodsId(goods.getGoodsId());
            if(StringUtils.isEmpty(oldGoodsParamDataList)){
                goodsParamDataMapper.insertBatch(goods.getGoodsParamDataList());
                return;
            }
            Map<Integer, List<TsGoodsParamData>> listDiff = DifUtils.findListDiff(goods.getGoodsParamDataList(), oldGoodsParamDataList);
            if (StringUtils.isNotEmpty(listDiff.get(0))){
                goodsParamDataMapper.insertBatch(listDiff.get(0));
            }
            if (StringUtils.isNotEmpty(listDiff.get(1))){
                goodsParamDataMapper.updateBatch(listDiff.get(1));
            }
            if (StringUtils.isNotEmpty(listDiff.get(2))){
                goodsParamDataMapper.deleteBatch(listDiff.get(2));
            }
        }else{
            goodsParamDataMapper.deleteByGoodsId(goods.getGoodsId());
        }
    }

    private void batchGoodsId(TsGoods goods){
        @Valid List<TsGoodsParamData> list = goods.getGoodsParamDataList();
        list.forEach((item)->{
            item.setGoodsId(goods.getGoodsId());
        });
    }
}
